<template>
    <div>
        <p>时间范围<el-time-picker class="order-input" is-range value-format="yyyy-MM-dd HH:mm:ss" v-model="time" range-separator="至" start-placeholder="开始时间" end-placeholder="结束时间" placeholder="选择时间范围">
  </el-time-picker><el-button type="primary" @click="refertotal">查询</el-button></p>

  <div class="data-box" id="chart"></div>
    </div>
</template>

<script>
import * as echarts from 'echarts';
import {ordertotal} from '@/apis/echart';
import {TimeTransition} from '@/utils/utils'

    export default {
        data(){
            return{
                 time: [],
                 orderDate:[],
                 orderdata:[],
            }
        },
        methods:{
            //echarts表格
            gettotal(date,data){
            let myChart = echarts.init(document.getElementById('chart'));
            myChart.setOption({
                    title: {
        text: '订单统计',
        left: 10
    },
                   tooltip: {
                   trigger: 'axis',
                   axisPointer: {
                   type: 'cross',
                   crossStyle: {
                     color: '#999'
                    }
                   }
                },
    toolbox: {
        feature: {
            dataView: {show: true, readOnly: false},
            magicType: {show: true, type: ['line', 'bar']},
            restore: {show: true},
            saveAsImage: {show: true}
        }
    },
    legend: {
        data: ['金额','销量']
    },
    xAxis: [
        {
            type: 'category',
            data: date,
            axisPointer: {
                type: 'shadow'
            },
            
        }
    ],
    yAxis: [
        {
            type: 'value',
            name: '金额',
            min: 0,
            max: 150,
            interval: 30,
            
        },
        {
            type: 'value',
            name: '',
            min: 0,
            max: 500,
            interval: 50,
        }
        
    ],
    series: [
        {
            name: '金额',
            type: 'bar',
            data: data
        },
        {
            name: '销量',
            type: 'line',
            data:["50","40","45","52","58","90","104","98","120","115","210","180","146","120","101","95","98","122","150","140","130"]
        }
    ]
});
            },
            //查询按钮
            refertotal(){
                 ordertotal({date:JSON.stringify(this.time)}).then(res=>{
                this.orderDate=res.data.data.map(v=>{
                    return TimeTransition(v.orderTime)
                })
                this.orderdata=res.data.data.map(v=>{
                    return v.orderAmount
                })
                this.gettotal(this.orderDate,this.orderdata)
            })
            },
        },
        mounted(){
            ordertotal({date:JSON.stringify(this.time)}).then(res=>{
                this.orderDate=res.data.data.map(v=>{
                    return TimeTransition(v.orderTime)
                })
                this.orderdata=res.data.data.map(v=>{
                    return v.orderAmount
                })
                this.gettotal(this.orderDate,this.orderdata)
            })
         

        }
    }
</script>

<style lang="less" scoped>
.order-input{
    margin: 0 5px;
}
p{
    margin-top: 20px;
    margin-bottom: 50px;
}
.data-box{
    width: 95%;
    height: 500px;
}
</style>